package com.caiyi.financial.nirvana.sms.gateway.sgip;

import com.caiyi.financial.nirvana.sms.gateway.sgip.handler.SGIPServerControlHandler;
import com.caiyi.financial.nirvana.sms.gateway.sgip.handler.SGIPServerHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.timeout.IdleStateHandler;

import java.util.concurrent.TimeUnit;

public class SGIPServerInitializer extends ChannelInitializer<SocketChannel> {

    private SGIPClient client;

    //构造
    public SGIPServerInitializer(SGIPClient client) {
        this.client = client;
    }

    @Override
    protected void initChannel(SocketChannel channel) {

        ChannelPipeline pipeline = channel.pipeline();

        //长度解码器
        pipeline.addLast("length field decoder", new LengthFieldBasedFrameDecoder(1024, 0, 4, -4, 0));

        //解码器
        pipeline.addLast("decoder", new SGIPStreamDecoder());

        //编码器
        pipeline.addLast("encoder", new SGIPStreamEncoder());

        //空闲事件将发送UP事件
        pipeline.addLast("idleCheck", new IdleStateHandler(client.idleNotifyTime * 2, -1, -1, TimeUnit.SECONDS));

        //接收控制器
        pipeline.addLast("receiver handler", new SGIPServerHandler(client));

        //总控制器
        pipeline.addLast("controler", new SGIPServerControlHandler());


    }

}
